/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import static org.junit.Assert.*;

import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.Assert.*;


/**
 *
 * @author Andry
 */
public class TokoServiceJDBCTest {
    
    private static Connection connection;
    private static TokoServiceJDBC tokoservice;

    @BeforeClass
    public static void inisialisasi() throws ClassNotFoundException, SQLException{

            // 0.Variable
            String dbDriver="com.mysql.jdbc.Driver";
            String url ="jdbc:mysql://localhost:3306/belajar";
            String user="root";
            String pass="";
            String sql ="select * from produk";

            // 1.Register
            Class.forName(dbDriver);

            // 2.Buka koneksi
            connection = DriverManager.getConnection(url,user,pass);

            // 3.instankan tokoservice
            tokoservice = new TokoServiceJDBC(connection);
    }

    @AfterClass
    public static void disconect() throws SQLException{
        connection.close();
    }

    @Before
    public void resetDatabase() throws SQLException{
        String sql= "delete from produk";
        connection.createStatement().executeUpdate(sql);

        String sqlInsert = "insert into produk" +
                "(id,kode,judul,pengarang,penerbit,harga)" +
                "values (100,'J-003','Java database Connectivity','Endy','Penerbit AAA',15750.00)";
        connection.createStatement().executeUpdate(sqlInsert);
    }

//    @Test
    public void testCariBukuById(){
        //buku yg sudah ada di database
        Buku p = tokoservice.cariID(100);
        assertNotNull(p);

        //buku yang belum ada di database
        Buku p2 = tokoservice.cariID(500);
        assertNull(p2);
    }

//    @Test
    public void testCariBukuByKode(){
        List<Buku> hasil = tokoservice.cariKode("J-003");
        assertNotNull(hasil);
        assertTrue(hasil.size()==1);

        //jumlah record sudah benar, mari periksa contentnya
        Buku b = hasil.get(0);
        assertEquals(new Integer(100), b.getId());
        assertEquals("Java database Connectivity",b.getJudul());
        assertEquals("Endy",b.getPengarang());
        assertEquals("Penerbit AAA",b.getPenerbit());
        assertEquals(new BigDecimal(15750).setScale(2), b.getHarga());

        //test untuk kode yang tidak ada di database
        List<Buku> hasil2 = tokoservice.cariKode("X-003");
        assertNotNull(hasil2);
        assertTrue(hasil2.isEmpty());
    }


    public void testCariBukuByJudul(){
        List<Buku> hasil = tokoservice.cariJudul("Java");
        assertNotNull(hasil);
        assertTrue(hasil.size()==1);

        //jumlah record sudah benar, mari periksa contentnya
        Buku b = hasil.get(0);
        assertEquals(new Integer(100), b.getId());
        assertEquals("Java database Connectivity",b.getJudul());
        assertEquals("Endy",b.getPengarang());
        assertEquals("Penerbit AAA",b.getPenerbit());
        assertEquals(new BigDecimal(15750).setScale(2), b.getHarga());

        //test untuk kode yang tidak ada di database
        List<Buku> hasil2 = tokoservice.cariJudul("Sumatra");
        assertNotNull(hasil2);
        assertTrue(hasil2.isEmpty());
    }

    //@Test
    public void testSimpan() throws SQLException{
        Buku p = new Buku();
        p.setKode("J-001");
        p.setJudul("Training Java");
        p.setPengarang("Endy Muhardin");
        p.setPenerbit("Telkom Sigma");
        p.setHarga(new BigDecimal(10000.00));

        tokoservice.simpan(p);

        // cek udah masuk atau belum yang ada di tabel
        cekJumlahBuku(2);
    }

    //@Test
    public void testDelete() throws SQLException{
            Buku b = new Buku();
            b.setId(100);

            tokoservice.hapus(b);
            cekJumlahBuku(0);
        }

    @Test
    public void testImport() throws Exception{
        String file ="C:/Users/Andry/Documents/NetBeansProjects/Season2/test/buku.csv";
        
        tokoservice.importCsv(file);
        cekJumlahBuku(1);
    }


    public void testSimpanPenjualan(){
        Buku b = tokoservice.cariID(100);

        PenjualanDetail pd = new PenjualanDetail();
        Penjualan p = new Penjualan();

        pd.setBuku(b);
        pd.setQty(10);
        pd.setPenjualan(p);

        p.setKasir("andry");
        p.setPembayaran(new BigDecimal(1500000));
        p.setWaktu(new Date());
        p.getDaftarPenjualanDetail().add(pd);

        tokoservice.simpan(p);

    }

    public void cekJumlahBuku(Integer seharusnya) throws SQLException{
        String sql ="select count(*) from produk";
        ResultSet rs = connection.createStatement().executeQuery(sql);
        assertTrue(rs.next());
        assertEquals(new Integer(seharusnya),new Integer(rs.getInt(1)));
    }   
}
